package com.seek.eduorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seek.commonutils.ResponseResult;
import com.seek.eduorder.entity.Order;
import com.seek.eduorder.entity.vo.OrderQuery;
import com.seek.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: wml
 * @since: 2022/3/21 22:34
 */
@RestController
@RequestMapping("/eduorder/order/admin")
public class OrderAdminController {

    @Autowired
    private OrderService orderService;

    // 条件查询带分页的方法
    @GetMapping("pageCondition/{current}/{size}")
    public ResponseResult pageCondition(@PathVariable int current, @PathVariable int size,
                                        OrderQuery orderQuery) {
        Page<Order> page = new Page<>(current, size);
        // 构建条件
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        String orderNo = orderQuery.getOrderNo();
        String courseTitle = orderQuery.getCourseTitle();
        String teacherName = orderQuery.getTeacherName();
        String nickname = orderQuery.getNickname();
        String mobile = orderQuery.getMobile();
        Integer payType = orderQuery.getPayType();
        Integer status = orderQuery.getStatus();
        String begin = orderQuery.getBegin();
        String end = orderQuery.getEnd();
        // 判断条件值是否为空，如果不为空拼接条件
        if (!(orderNo == null || orderNo.equals(""))) {
            queryWrapper.like("order_no", orderNo);
        }
        if (!(courseTitle == null || courseTitle.equals(""))) {
            queryWrapper.like("course_title", courseTitle);
        }
        if (!(teacherName == null || teacherName.equals(""))) {
            queryWrapper.like("teacher_name", teacherName);
        }
        if (!(nickname == null || nickname.equals(""))) {
            queryWrapper.like("nickname", nickname);
        }
        if (!(mobile == null || mobile.equals(""))) {
            queryWrapper.like("mobile", mobile);
        }
        if (payType != null) {
            queryWrapper.eq("pay_type", payType);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        if (!(begin == null || begin.equals(""))) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!(end == null || end.equals(""))) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        orderService.page(page, queryWrapper);
        long total = page.getTotal();
        List<Order> records = page.getRecords();
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 根据订单 id 进行查询
    @GetMapping("{id}")
    public ResponseResult getById(@PathVariable String id) {
        Order order = orderService.getById(id);
        return ResponseResult.ok().data("order", order);
    }
}
